{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: gensim in c:\\programdata\\anaconda3\\lib\\site-packages (4.2.0)\n",
      "Requirement already satisfied: numpy>=1.17.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gensim) (1.21.6)\n",
      "Requirement already satisfied: scipy>=0.18.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from gensim) (1.1.0)\n",
      "Requirement already satisfied: smart-open>=1.8.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from gensim) (7.1.0)\n",
      "Requirement already satisfied: Cython==0.29.28 in c:\\programdata\\anaconda3\\lib\\site-packages (from gensim) (0.29.28)\n",
      "Requirement already satisfied: wrapt in c:\\programdata\\anaconda3\\lib\\site-packages (from smart-open>=1.8.1->gensim) (1.10.11)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "DEPRECATION: pandas 0.23.4 has a non-standard dependency specifier pytz>=2011k. pip 24.1 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pandas or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\n"
     ]
    }
   ],
   "source": [
    "!pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gensim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gensim\n",
    "from gensim.models.doc2vec import Doc2Vec\n",
    "#从gensim.models.doc2vec模块中导入TaggedDocument类\n",
    "TaggededDocument = gensim.models.doc2vec.TaggedDocument\n",
    "def get_data():     #定义读取数据函数\n",
    "    with open(\"C:/Users/Administrator/Desktop/train.txt\",'r',encoding='utf-8') as f:\n",
    "        docs = f.readlines()  #读取每行数据\n",
    "    train_data = []\n",
    "    for i, text in enumerate(docs):\n",
    "        word_list = text.split(' ')  #按空格分割\n",
    "        #删除每行最后一个单词后的空余空格或换行符\n",
    "        word_list[len(word_list)-1] = word_list[len(word_list)-1].strip()\n",
    "        #使用词列表和文档索引作为参数，实例化TaggededDocument类的对象\n",
    "        document = TaggededDocument(word_list, tags =[i])\n",
    "        train_data.append(document)\n",
    "    return train_data\n",
    "def train_model(x_train):    #训练Doc2Vec模型\n",
    "    #创建Doc2Vec类的对象\n",
    "    model_dm = Doc2Vec(x_train, min_count=1, window=3, vector_size=20, negative=5, workers=4, dm=1)\n",
    "    model_dm.train(x_train, total_examples=model_dm.corpus_count, epochs=70)    #训练模型\n",
    "    model_dm.save(\"data/model_doc2vec\")   #保存模型\n",
    "    return model_dm\n",
    "def test():\n",
    "    model_dm=Doc2Vec.load(\"data/model_doc2vec\")   #加载模型\n",
    "    #定义一个测试文本列表\n",
    "    test_text=['科学','教育','是','难搞','的']\n",
    "    #使用模型推断测试文本的向量表示\n",
    "    inferred_vector_dm=model_dm.infer_vector(test_text)\n",
    "    sims = model_dm.docvecs.most_similar([inferred_vector_dm],topn=10)  #找到与测试文本向量最相似的其他文本向量\n",
    "    return sims\n",
    "if __name__ =='__main__':\n",
    "    train_data = get_data()\n",
    "    model_dm = train_model(train_data)\n",
    "    sims = test()\n",
    "    print(\"相似文本、相似度和文本中词的数量：\")\n",
    "    for count,sim in sims:     #遍历相似度列表\n",
    "        sentence = train_data[count]   #获取相似度最高的文本\n",
    "        words = ''\n",
    "        for word in sentence[0]:\n",
    "            words = words + word + ''\n",
    "        #输出相似文本、相似度和文本中词的数量\n",
    "        print(words,sim,len(sentence[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
